package org.proddevplm.business.tools;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.proddevplm.business.api.AController;
import org.proddevplm.business.tools.constants.ToolsConstants;
import org.proddevplm.constants.ControllerConstants;
import org.proddevplm.constants.UserConstants;
import org.proddevplm.dao.data.Product;
import org.proddevplm.dao.data.RelationMatrix;
import org.proddevplm.dao.data.Roles;
import org.proddevplm.dao.data.User;
import org.proddevplm.dao.data.activities.Activity;
import org.proddevplm.dao.data.changeReport.ChangeReport;
import org.proddevplm.dao.data.competitors.Competitors;
import org.proddevplm.dao.data.competitors.CompetitorsProduct;
import org.proddevplm.dao.data.conceptSelection.PughAnalysis;
import org.proddevplm.dao.data.conceptSelection.PughCriteria;
import org.proddevplm.dao.data.correlations.QFD;
import org.proddevplm.dao.data.designForX.DesignForX;
import org.proddevplm.dao.data.designForX.DfXGImportances;
import org.proddevplm.dao.data.designForX.DfXGuidelines;
import org.proddevplm.dao.data.designForX.DfXImportances;
import org.proddevplm.dao.data.designForX.DfXRelMatrix;
import org.proddevplm.dao.data.designInputs.CTQs;
import org.proddevplm.dao.data.designInputs.DesignElements;
import org.proddevplm.dao.data.designInputs.ProductFunctions;
import org.proddevplm.dao.data.prioritize.AHPAnalysis;
import org.proddevplm.dao.data.requirements.Requirements;
import org.proddevplm.dao.data.requirements.VOCTable;
import org.proddevplm.dao.data.stakeholders.Stakeholder;
import org.proddevplm.dao.data.swot.SwotAnalysis;
import org.proddevplm.helpers.ToolsUtil;

public class ToolsController extends AController{

	@Override
	public Object execute(int command, Object data) {

		Map<String, Object> analysisMap = (Map<String, Object>) data;
		Product currentProduct = (Product)getSession().getAttribute("currentProduct");
		int productId = (Integer) getSession().getAttribute("productId");
		int projectId = (Integer)getSession().getAttribute("projectId");
		String[] competitorsArray;
		User user = (User)getSession().getAttribute(UserConstants.USER);
		List<RelationMatrix> relMatrix = (List<RelationMatrix>)getSession().getAttribute("relMatrixUser");
		RelationMatrix rm = (RelationMatrix)relMatrix.get(0);
		String currentRole = "";
		for (RelationMatrix r:relMatrix){
			if (r.getProject().getId()==projectId){
				Roles role = r.getRole();
				currentRole = role.getRole();
			}
		}
		
		Activity currentActivity;
		String activityName = "";
		
				
		switch (command){
		case ToolsConstants.COMPETITORS_ANALYSIS_FORM1:
			
			try{
				if((!((String)analysisMap.get("productConcept")).equals(""))&&(!((String)analysisMap.get("productFamily")).equals(""))){
					currentProduct.setConcept((String)analysisMap.get("productConcept"));
					currentProduct.setFamily((String)analysisMap.get("productFamily"));
					getDaoManager().getProductManager().mergeProductInfo(currentProduct);
				}
					
				competitorsArray = (String[]) analysisMap.get("competitorsArray");
				for(int i=0; i<competitorsArray.length; i++){
					if(!competitorsArray[i].equals("")){
						Competitors competitor = new Competitors();
						competitor.setProductId(productId);
						competitor.setName(competitorsArray[i]);
						getDaoManager().getToolsManager().addCompetitor(competitor);	
					}
				}
				List<Competitors> competitorsByProduct = getDaoManager().getDataAccessManager().getCompetitorByProductId(productId);
				putOnSession("competitorsByProduct", competitorsByProduct);
				
			}catch(Exception e){
				e.printStackTrace();
			}
			
			putOnState(ControllerConstants.NEXT_PAGE, "content/project/projectPage.jsp");
            putOnSession("currentTab", "tab5");
            break;
            
		case ToolsConstants.COMPETITORS_ANALYSIS_FORM2:
			
			try{
				CompetitorsProduct competitorProduct = new CompetitorsProduct();
				
				if(((String)analysisMap.get("compProductName")!="")&&((String)analysisMap.get("competitor")!="")&&((String)analysisMap.get("characteristicsList")!="")){
					competitorProduct.setCompetitorProduct((String)analysisMap.get("compProductName"));
					competitorProduct.setCompetitor((String)analysisMap.get("competitor"));
					competitorProduct.setTechnicalCharacteristics((String)analysisMap.get("characteristicsList"));
					
					competitorProduct.setActivityName(analysisMap.get("activityName").toString());
					competitorProduct.setProductId(productId);
					getDaoManager().getToolsManager().addCompProduct(competitorProduct);
				}
				
				//de gasit solutie pentru pozitionarea call-ului
	            List<CompetitorsProduct> cpResults = getDaoManager().getDataAccessManager().getCompetitorsProductByProductId(productId);
	          	putOnSession("cpResults", cpResults);
				
			}catch(Exception e){
				e.printStackTrace();
			}
			
			putOnState(ControllerConstants.NEXT_PAGE, "content/project/projectPage.jsp");
            putOnSession("currentTab", "tab5");
            
            
			break;
			
		case ToolsConstants.SWOT:
			
			try{
				if (!((String)analysisMap.get("analysisName")).equals("")){
					SwotAnalysis swotAnalysis = new SwotAnalysis();
					swotAnalysis.setAuthor(user.getSurname()+" "+user.getName());
					swotAnalysis.setName((String)analysisMap.get("analysisName"));
					swotAnalysis.setCompetitor((String)analysisMap.get("competitor"));
					swotAnalysis.setCompProduct((String)analysisMap.get("product"));
					swotAnalysis.setStrengths((String)analysisMap.get("strengthsList"));
					swotAnalysis.setWeaknesses((String)analysisMap.get("weaksList"));
					swotAnalysis.setOpportunities((String)analysisMap.get("oppsList"));
					swotAnalysis.setThreats((String)analysisMap.get("threatsList"));
					swotAnalysis.setActivityName(analysisMap.get("activityName").toString());
					swotAnalysis.setProductId(productId);
					
					getDaoManager().getToolsManager().addSwotAnalysis(swotAnalysis);
					
					currentActivity = getDaoManager().getActivityManager().getActivityByNameandProductId(analysisMap.get("activityName").toString(), productId);
					if(currentActivity!=null){
						currentActivity.setActivityLifeCycle("Under review");
						getDaoManager().getActivityManager().updateActivity(currentActivity);
					}
				}
			}catch(Exception e){
				e.printStackTrace();
			}
			putOnState(ControllerConstants.NEXT_PAGE, "content/project/projectPage.jsp");
            putOnSession("currentTab", "tab5");
			break;
            
		case ToolsConstants.SWOT_FORWARDING:
			putOnState(ControllerConstants.NEXT_PAGE, "content/project/projectPage.jsp");
            putOnSession("currentTab", "tab4");
            break;
				
		case ToolsConstants.STAKEHOLDERS:
			String response="";
			
			try{
				if(!((String)analysisMap.get("shName")).equals("")){
					if(getDaoManager().getDataAccessManager().getStakeholdersByNameandProdId(analysisMap.get("shName").toString(), productId)){
						response = "Stakeholder's name already in the database";
					}
					else{
						Stakeholder stakeholder = new Stakeholder();
						
						stakeholder.setProductId(productId);
						stakeholder.setShName((String)analysisMap.get("shName"));
						stakeholder.setDetails((String)analysisMap.get("shDetails"));
						stakeholder.setType((String)analysisMap.get("type"));
						stakeholder.setLcStage((String)analysisMap.get("lcStages"));
						stakeholder.setActivityName((String)analysisMap.get("activityName"));
						stakeholder.setAuthor(user.getSurname()+" "+user.getName());
						
						getDaoManager().getToolsManager().addStakeholder(stakeholder);
						response = "Stakeholder "+(String)analysisMap.get("shName")+" saved in the database";
					}
				}
				else response = "Please enter a name for the stakeholder";
				
			}catch(Exception e){
				e.printStackTrace();
			}
			return response;
			
		case ToolsConstants.STAKEHOLDERS_FWD:
			putOnState(ControllerConstants.NEXT_PAGE, "content/project/projectPage.jsp");
            putOnSession("currentTab", "tab4");
            break;
            
		case ToolsConstants.REM_STAKEHOLDER:
			String shName = (String)analysisMap.get("shName");
			getDaoManager().getToolsManager().deleteStakeholderByName(shName, productId);
			break;
            
		case ToolsConstants.REQUIREMENTS:
			
			try{
				Requirements requirement = new Requirements();
				requirement.setProductId(productId);
				requirement.setStakeholder(currentRole);
				requirement.setRequirement(analysisMap.get("requirement").toString());
				requirement.setDescription(analysisMap.get("description").toString());
				requirement.setAuthor(user.getSurname()+" "+user.getName());
				requirement.setRole(currentRole);
				requirement.setStatus("original");
				requirement.setActivityName(analysisMap.get("activityName").toString());
					
				getDaoManager().getToolsManager().addRequirement(requirement);
				
			}catch(Exception e){
				e.printStackTrace();
			}
			
			putOnState(ControllerConstants.NEXT_PAGE, "content/project/projectPage.jsp");
            putOnSession("currentTab", "tab4");			
			break;
			
		case ToolsConstants.REM_REQUIREMENT:
			String requirement = (String)analysisMap.get("requirement");
			getDaoManager().getToolsManager().deleteReqByNameandProdId(requirement,productId);
			
			putOnState(ControllerConstants.NEXT_PAGE, "content/project/projectPage.jsp");
            putOnSession("currentTab", "tab5");	
			break;
			
		case ToolsConstants.EDIT_REQUIREMENT:
			int requirementId = Integer.valueOf((String)analysisMap.get("requirementId"));
			Requirements currentRequirement = getDaoManager().getDataAccessManager().getRequirementById(requirementId);
			try{
				currentRequirement.setRequirement(analysisMap.get("editedRequirement").toString());
				currentRequirement.setStatus("edited");
				currentRequirement.setDescription(analysisMap.get("editedReqDesc").toString());
				if(!("-Please select an option-".equals(analysisMap.get("newSHforReq")))){
					currentRequirement.setStakeholder(analysisMap.get("newSHforReq").toString());
				}
				getDaoManager().getToolsManager().updateRequirement(currentRequirement);
			}catch(Exception e){
				e.printStackTrace();
			}
			putOnState(ControllerConstants.NEXT_PAGE, "content/project/projectPage.jsp");
            putOnSession("currentTab", "tab5");	
			return currentRequirement.getRequirement()+"~"+currentRequirement.getAuthor()+" / e~"+currentRequirement.getDescription()+"~"+currentRequirement.getStakeholder();
			
		case ToolsConstants.ADD_EXTRA_REQUIREMENT:
			Requirements extraReq = new Requirements();
			try{
				extraReq.setProductId(productId);
				extraReq.setStakeholder((String)analysisMap.get("stakeholder"));
				extraReq.setRequirement((String)analysisMap.get("requirement"));
				extraReq.setActivityName((String)analysisMap.get("activityName"));
				extraReq.setDescription(analysisMap.get("description").toString());
				extraReq.setAuthor(user.getSurname()+" "+user.getName());
				extraReq.setRole(currentRole);
				extraReq.setStatus("original");
				
				getDaoManager().getToolsManager().addRequirement(extraReq);
				
			}catch(Exception e){
				e.printStackTrace();
			}
			putOnState(ControllerConstants.NEXT_PAGE, "content/project/projectPage.jsp");
            putOnSession("currentTab", "tab5");	
			return extraReq.getStakeholder()+"~"+extraReq.getRequirement()+"~"+extraReq.getAuthor()+"~"+extraReq.getRole()+"~"+extraReq.getDescription();
		
		case ToolsConstants.PRIORITIZE:
			
			int matrixSize = Integer.valueOf((String)analysisMap.get("matrixSize"));
			float[][] ahpMatrix = (float[][]) analysisMap.get("ahpMatrix");
			
			float[] ahpResults = ToolsUtil.ahpMatrixCalculus(matrixSize, ahpMatrix);
			String[] elements = (String[])analysisMap.get("elements");
			String dataType = analysisMap.get("dataType").toString();
			if("stakeholders".equals(dataType)){
				for(int i=0; i<elements.length; i++){
					Stakeholder sh = getDaoManager().getDataAccessManager().getSHByNameandProdId(elements[i], productId);
					sh.setImportance(Float.toString(ahpResults[i]));
					getDaoManager().getToolsManager().updateStakeholder(sh);
				}
			}else if("requirements".equals(dataType)){
				for(int i=0; i<elements.length; i++){
					Requirements req = getDaoManager().getDataAccessManager().getReqByNameandProdId(elements[i], productId);
					req.setImportance(Float.toString(ahpResults[i]));
					getDaoManager().getToolsManager().updateRequirement(req);
				}
			}
			
			putOnSession("ahpResults", ahpResults);
			
			putOnState(ControllerConstants.NEXT_PAGE, "content/project/projectPage.jsp");
            putOnSession("currentTab", "tab4");
            String criteriaImportances="";
            for(int i=0; i<ahpResults.length; i++){
            	criteriaImportances+=ahpResults[i]+"~";
            }
            criteriaImportances = criteriaImportances.substring(0, criteriaImportances.length()-1);
            
            return criteriaImportances;
			
		case ToolsConstants.SUBMIT_AHP:
			String ret ="";
			AHPAnalysis ahpAnalysis = new AHPAnalysis();
			String importances = "";
			float[] results = (float[]) getSession().getAttribute("ahpResults");
			for(int i=0; i<results.length; i++){
				importances+=results[i]+"~";
			}
			importances = importances.substring(0, importances.length()-1);
						
			try{
				ahpAnalysis.setProductId(productId);
				ahpAnalysis.setName((String)analysisMap.get("ahpAnalysisName"));
				ahpAnalysis.setActivityName(analysisMap.get("activityName").toString());
				ahpAnalysis.setDate(new Date().toString());
				ahpAnalysis.setElements((String)analysisMap.get("elements"));
				ahpAnalysis.setImportances(importances);
				ahpAnalysis.setMatrixValues((String)analysisMap.get("ahpMatrix"));
				ahpAnalysis.setAuthor(user.getSurname()+" "+user.getName());
				
				getDaoManager().getToolsManager().addAhpAnalysis(ahpAnalysis);
				
			}catch(Exception e){
				e.printStackTrace();
			}
			return ret;
			
		case ToolsConstants.VOCT:
			VOCTable voct = new VOCTable();
			String message="";
			
			try{
				//conditie de verificare ca fieldurile sa fie completate
				if((!((String)analysisMap.get("whats")).equals(""))&&(!((String)analysisMap.get("hows")).equals(""))&&(!((String)analysisMap.get("whys")).equals(""))&&(!((String)analysisMap.get("whos")).equals(""))){
				
					voct.setProductId(productId);
					voct.setActivityName(analysisMap.get("activityName").toString());
					voct.setRequirement((String)analysisMap.get("requirement"));
					voct.setStakeholder((String)analysisMap.get("stakeholder"));
					voct.setWhats((String)analysisMap.get("whats"));
					voct.setHows((String)analysisMap.get("hows"));
					voct.setWhys((String)analysisMap.get("whys"));
					voct.setWhos((String)analysisMap.get("whos"));
					voct.setAuthor(user.getSurname()+" "+user.getName());
					voct.setDate(new Date().toString());
					
					getDaoManager().getToolsManager().addVOCT(voct);
					message = "Information successfully added in the database";
				}
				else{
					message = "Please fill in all the required information";
				}
				
			}catch(Exception e){
				e.printStackTrace();
			}	
			putOnState(ControllerConstants.NEXT_PAGE, "content/project/projectPage.jsp");
            putOnSession("currentTab", "tab5");	
			
            return message;
            
		case ToolsConstants.FUNCTIONS:
			String[] functionsArray = (String[]) analysisMap.get("functionsArray");
			activityName = analysisMap.get("activityName").toString();
			
			try{
				for(int i=0; i<functionsArray.length; i++){
					ProductFunctions pf = new ProductFunctions();
					pf.setProductId(productId);
					pf.setAuthor(user.getSurname()+" "+user.getName());
					pf.setDate(new Date().toString());
					pf.setFunction(functionsArray[i]);
					pf.setActivityName(activityName);
					
					getDaoManager().getToolsManager().addProductFunction(pf);
				}
				currentActivity = getDaoManager().getActivityManager().getActivityByNameandProductId(analysisMap.get("activityName").toString(), productId);
				if(currentActivity!=null){
					currentActivity.setActivityLifeCycle("Under review");
					getDaoManager().getActivityManager().updateActivity(currentActivity);
				}
			}catch(Exception e){
				e.printStackTrace();
			}
			putOnState(ControllerConstants.NEXT_PAGE, "content/project/projectPage.jsp");
            putOnSession("currentTab", "tab4");	
            break;
            
		case ToolsConstants.CTQS:
			String[] ctqsArray = (String[]) analysisMap.get("ctqsArray");
			activityName = analysisMap.get("activityName").toString();
			
			try{
				for(int i=0; i<ctqsArray.length; i++){
					CTQs ctq = new CTQs();
					ctq.setProductId(productId);
					ctq.setAuthor(user.getSurname()+" "+user.getName());
					ctq.setDate(new Date().toString());
					ctq.setCtq(ctqsArray[i]);
					ctq.setActivityName(activityName);
					
					getDaoManager().getToolsManager().addCTQ(ctq);
				}
				currentActivity = getDaoManager().getActivityManager().getActivityByNameandProductId(analysisMap.get("activityName").toString(), productId);
				if(currentActivity!=null){
					currentActivity.setActivityLifeCycle("Under review");
					getDaoManager().getActivityManager().updateActivity(currentActivity);
				}
			}catch(Exception e){
				e.printStackTrace();
			}
			putOnState(ControllerConstants.NEXT_PAGE, "content/project/projectPage.jsp");
            putOnSession("currentTab", "tab4");	
            break;
            
		case ToolsConstants.DE:
			String[] desArray = (String[]) analysisMap.get("desArray");
			activityName = analysisMap.get("activityName").toString();
			
			try{
				for(int i=0; i<desArray.length; i++){
					DesignElements de = new DesignElements();
					de.setProductId(productId);
					de.setAuthor(user.getSurname()+" "+user.getName());
					de.setDate(new Date().toString());
					de.setDesignElement(desArray[i]);
					de.setActivityName(activityName);
					
					getDaoManager().getToolsManager().addDesignElement(de);
				}
				currentActivity = getDaoManager().getActivityManager().getActivityByNameandProductId(analysisMap.get("activityName").toString(), productId);
				if(currentActivity!=null){
					currentActivity.setActivityLifeCycle("Under review");
					getDaoManager().getActivityManager().updateActivity(currentActivity);
				}
			}catch(Exception e){
				e.printStackTrace();
			}
			putOnState(ControllerConstants.NEXT_PAGE, "content/project/projectPage.jsp");
            putOnSession("currentTab", "tab4");	
            break;
            
		case ToolsConstants.UPDATE_SWOT:
			SwotAnalysis currentSwot = getDaoManager().getDataAccessManager().getSwotByNameandProdId(analysisMap.get("swotName").toString(), productId);
			String swotMessage = "";
			if(currentSwot!=null){
				currentSwot.setStatus(true);
				getDaoManager().getToolsManager().updateSwotAnalysis(currentSwot);
				swotMessage = "Analysis approved";
			}
			return swotMessage;
			
		case ToolsConstants.REMOVE_FUNCTION:
			getDaoManager().getToolsManager().deleteProductFunction(analysisMap.get("functionName").toString(), productId);
			
			putOnState(ControllerConstants.NEXT_PAGE, "content/project/projectPage.jsp");
            putOnSession("currentTab", "tab5");
            break;
            
		case ToolsConstants.REMOVE_CTQ:
			getDaoManager().getToolsManager().deleteCTQ(analysisMap.get("ctqName").toString(), productId);
			
			putOnState(ControllerConstants.NEXT_PAGE, "content/project/projectPage.jsp");
            putOnSession("currentTab", "tab5");
            break;
            
		case ToolsConstants.REMOVE_DE:
			getDaoManager().getToolsManager().deleteDE(analysisMap.get("deName").toString(), productId);
			
			putOnState(ControllerConstants.NEXT_PAGE, "content/project/projectPage.jsp");
            putOnSession("currentTab", "tab5");
            break;
            
		case ToolsConstants.ADD_CHANGE_REPORT:
			try{
				ChangeReport cr = new ChangeReport();
				cr.setActivityName(analysisMap.get("activityName").toString());
				cr.setSubject(analysisMap.get("changeSubject").toString());
				cr.setDescription(analysisMap.get("changeDetails").toString());
				cr.setProductId(productId);
				cr.setAuthor(user.getSurname()+" "+user.getName());
				cr.setDate(new Date().toString());
				
				getDaoManager().getToolsManager().addChangeReport(cr);
				currentActivity = getDaoManager().getActivityManager().getActivityByNameandProductId(analysisMap.get("activityName").toString(), productId);
				currentActivity.setActivityLifeCycle("Rejected");
				getDaoManager().getActivityManager().updateActivity(currentActivity);
				
			}catch(Exception e){
				e.printStackTrace();
			}
			
			putOnState(ControllerConstants.NEXT_PAGE, "content/project/projectPage.jsp");
            putOnSession("currentTab", "tab4");
            break;
            
		case ToolsConstants.PUGH_CRITERIA:
			try{
				PughAnalysis existingPugh = getDaoManager().getDataAccessManager().getPughByActNameandProdId(analysisMap.get("activityName").toString(), productId);
				
				if(existingPugh == null){
					PughAnalysis pugh = new PughAnalysis();
					pugh.setActivityName(analysisMap.get("activityName").toString());
					pugh.setProductId(productId);
					pugh.setCriteria(analysisMap.get("criteria").toString());
					
					getDaoManager().getToolsManager().addPughAnalysis(pugh);
				}else{
					existingPugh.setActivityName(analysisMap.get("activityName").toString());
					existingPugh.setProductId(productId);
					existingPugh.setCriteria(analysisMap.get("criteria").toString());
					
					getDaoManager().getToolsManager().addPughAnalysis(existingPugh);
				}
				
			}catch(Exception e){
				e.printStackTrace();
			}
			putOnState(ControllerConstants.NEXT_PAGE, "content/project/projectPage.jsp");
            putOnSession("currentTab", "tab5");
            break;
            
		case ToolsConstants.PUGH_CRITERIA_HIERARCHY:
			try{
				PughAnalysis existingPugh = getDaoManager().getDataAccessManager().getPughByActNameandProdId(analysisMap.get("activityName").toString(), productId);
				
				existingPugh.setCriteriaImportances(analysisMap.get("criteriaImportances").toString());
				getDaoManager().getToolsManager().addPughAnalysis(existingPugh);
				
			}catch(Exception e){
				e.printStackTrace();
			}
			putOnState(ControllerConstants.NEXT_PAGE, "content/project/projectPage.jsp");
            putOnSession("currentTab", "tab5");
            break;
            
		case ToolsConstants.PUGH_CONCEPTS:
			try{
				PughAnalysis existingPugh = getDaoManager().getDataAccessManager().getPughByActNameandProdId(analysisMap.get("activityName").toString(), productId);
				
				existingPugh.setConcepts(analysisMap.get("concepts").toString());
				getDaoManager().getToolsManager().addPughAnalysis(existingPugh);
				
			}catch(Exception e){
				e.printStackTrace();
			}
			putOnState(ControllerConstants.NEXT_PAGE, "content/project/projectPage.jsp");
            putOnSession("currentTab", "tab5");
            break;
            
		case ToolsConstants.PUGH_CALCULUS:
			String positives = "";
			String negatives = "";
			String conceptsImportances = "";
			
			try{
				PughAnalysis existingPugh = getDaoManager().getDataAccessManager().getPughByActNameandProdId(analysisMap.get("activityName").toString(), productId);
				
				String criteria = existingPugh.getCriteria();
				String[] criteriaArray = criteria.split("~");
				String concepts = existingPugh.getConcepts();
				String[] conceptsArray = concepts.split("~");
				String pughCriteriaImportances = existingPugh.getCriteriaImportances();
				String[] pughCIArray = pughCriteriaImportances.split("~");
				
				Map<String, Object> pughMap = ToolsUtil.pughMatrixCalculus(criteriaArray.length, conceptsArray.length, pughCIArray, analysisMap.get("pughMatrixValues").toString());
				
				
				float[] positiveEffects = (float[])pughMap.get("positiveEffects");
				float[] negativeEffects = (float[])pughMap.get("negativeEffects");
				float[] netEffects = (float[])pughMap.get("netEffects");
				for(int k=0; k<positiveEffects.length; k++){
					positives+=positiveEffects[k]+"~";
					negatives+=negativeEffects[k]+"~";
					conceptsImportances+=netEffects[k]+"~";
				}
				positives = positives.substring(0, positives.length()-1);
				negatives = negatives.substring(0, negatives.length()-1);
				conceptsImportances = conceptsImportances.substring(0, conceptsImportances.length()-1);
				
				existingPugh.setMatrixValues(analysisMap.get("pughMatrixValues").toString());
				existingPugh.setPositiveEffects(positives);
				existingPugh.setNegativeEffects(negatives);
				existingPugh.setConceptResults(conceptsImportances);
				
				getDaoManager().getToolsManager().addPughAnalysis(existingPugh);
				
			}catch(Exception e){
				e.printStackTrace();
			}
			
			putOnState(ControllerConstants.NEXT_PAGE, "content/project/projectPage.jsp");
            putOnSession("currentTab", "tab5");
			
            return positives+"&xkv"+negatives+"&xkv"+conceptsImportances;
            
		case ToolsConstants.QFD_CALCULUS:
			String qfdResultsString = "";
			String whats = "";
			String hows = "";
			try{
				String qfdMatrixValues = analysisMap.get("qfdMatrixValues").toString();
				String[] qfdMatrix = qfdMatrixValues.split("~");
				
				if("reqVsCtqs".equals(analysisMap.get("qfdType").toString())){
					String qfdType = analysisMap.get("qfdType").toString();
					List<Requirements> reqs = getDaoManager().getDataAccessManager().getRequirementsByProductId(productId);
					List<CTQs> ctqs = getDaoManager().getDataAccessManager().getCTQsByProductId(productId);
					
					float[] qfdResults = ToolsUtil.qfdMatrixCalculus(reqs, ctqs, qfdMatrix, qfdType, productId);
					for(int i=0; i<qfdResults.length; i++){
						CTQs ctq = ctqs.get(i);
						hows+=ctq.getCtq()+"~";
						ctq.setImportance(Float.valueOf(qfdResults[i]).toString());
						getDaoManager().getToolsManager().updateCTQ(ctq);
						qfdResultsString+=qfdResults[i]+"~";
					}
					for(int j=0; j<reqs.size(); j++){
						Requirements req = reqs.get(j);
						whats+=req.getRequirement()+"~";
					}
					qfdResultsString = qfdResultsString.substring(0, qfdResultsString.length()-1);
				
				}else if("ctqsVsFunctions".equals(analysisMap.get("qfdType").toString())){
					String qfdType = analysisMap.get("qfdType").toString();
					List<CTQs> ctqs = getDaoManager().getDataAccessManager().getCTQsByProductId(productId);
					List<ProductFunctions> functions = getDaoManager().getDataAccessManager().getFunctionsByProductId(productId);
					
					float[] qfdResults = ToolsUtil.qfdMatrixCalculus(ctqs, functions, qfdMatrix, qfdType, productId);
					for(int i=0; i<qfdResults.length; i++){
						ProductFunctions pf = functions.get(i);
						hows+=pf.getFunction()+"~";
						pf.setImportance(Float.valueOf(qfdResults[i]).toString());
						getDaoManager().getToolsManager().updateProductFunction(pf);
						qfdResultsString+=qfdResults[i]+"~";
					}
					for(int j=0; j<ctqs.size(); j++){
						CTQs ctq = ctqs.get(j);
						whats+=ctq.getCtq()+"~";
					}
					qfdResultsString = qfdResultsString.substring(0, qfdResultsString.length()-1);
				
				}else if("functionsVsDEs".equals(analysisMap.get("qfdType").toString())){
					String qfdType = analysisMap.get("qfdType").toString();
					List<ProductFunctions> functions = getDaoManager().getDataAccessManager().getFunctionsByProductId(productId);
					List<DesignElements> des = getDaoManager().getDataAccessManager().getDEByProductId(productId);
					
					float[] qfdResults = ToolsUtil.qfdMatrixCalculus(functions, des, qfdMatrix, qfdType, productId);
					for(int i=0; i<qfdResults.length; i++){
						DesignElements de = des.get(i);
						hows+=de.getDesignElement()+"~";
						de.setImportance(Float.valueOf(qfdResults[i]).toString());
						getDaoManager().getToolsManager().updateDesignElement(de);
						qfdResultsString+=qfdResults[i]+"~";
					}
					for(int j=0; j<functions.size(); j++){
						ProductFunctions pf = functions.get(j);
						whats+=pf.getFunction()+"~";
					}
					qfdResultsString = qfdResultsString.substring(0, qfdResultsString.length()-1);
				
				}else if("shVsDFX".equals(analysisMap.get("qfdType").toString())){
					String qfdType = analysisMap.get("qfdType").toString();
					List<Stakeholder> stakeholders = getDaoManager().getDataAccessManager().getStakeholdersByProductId(productId);
					List<DesignForX> dfxs = getDaoManager().getDataAccessManager().getDfxByProdId(productId);
					
					float[] qfdResults = ToolsUtil.qfdMatrixCalculus(stakeholders, dfxs, qfdMatrix, qfdType, productId);
					for(int i=0; i<qfdResults.length; i++){
						DesignForX dfx = dfxs.get(i);
						hows+=dfx.getDfxName()+"~";
						DfXImportances dfxI = new DfXImportances();
						Product product = getDaoManager().getProductManager().getProductById(productId);
						dfxI.setDfx(dfx);
						dfxI.setProduct(product);
						dfxI.setImportance(Float.valueOf(qfdResults[i]).toString());
						getDaoManager().getToolsManager().addDfXImportance(dfxI);
						qfdResultsString+=qfdResults[i]+"~";
					}
					for(int j=0; j<stakeholders.size(); j++){
						Stakeholder sh = stakeholders.get(j);
						whats+=sh.getShName()+"~";
					}
					qfdResultsString = qfdResultsString.substring(0, qfdResultsString.length()-1);
					
				}else if("dfxVsGuidelines".equals(analysisMap.get("qfdType").toString())){
					String qfdType = analysisMap.get("qfdType").toString();
					List<DesignForX> dfxs = getDaoManager().getDataAccessManager().getDfxByProdId(productId);
					List<DfXGuidelines> dfxGuidelines = getDaoManager().getDataAccessManager().getDfXGuidelinesByProdId(productId);
					
					float[] qfdResults = ToolsUtil.qfdMatrixCalculus(dfxs, dfxGuidelines, qfdMatrix, qfdType, productId);
					for(int i=0; i<qfdResults.length; i++){
						DfXGuidelines dfxG = dfxGuidelines.get(i);
						hows+=dfxG.getGuideline()+"~";
						DfXGImportances dfxGI = new DfXGImportances();
						Product product = getDaoManager().getProductManager().getProductById(productId);
						dfxGI.setDfxGuideline(dfxG);
						dfxGI.setProduct(product);
						dfxGI.setImportance(Float.valueOf(qfdResults[i]).toString());
						getDaoManager().getToolsManager().addDfXGImportance(dfxGI);
						qfdResultsString+=qfdResults[i]+"~";
					}
					for(int j=0; j<dfxs.size(); j++){
						DesignForX dfx = dfxs.get(j);
						whats+=dfx.getDfxName()+"~";
					}
					qfdResultsString = qfdResultsString.substring(0, qfdResultsString.length()-1);
				}
				
				whats = whats.substring(0, whats.length()-1);
				hows = hows.substring(0, hows.length()-1);
				QFD existingQFD = getDaoManager().getDataAccessManager().getQFDByActNameandProdId(analysisMap.get("activityName").toString(), productId);
				if(existingQFD == null){
					QFD qfd = new QFD();
					qfd.setActivityName(analysisMap.get("activityName").toString());
					qfd.setWhats(whats);
					qfd.setHows(hows);
					qfd.setMatrixValues(qfdMatrixValues);
					qfd.setProductId(productId);
					qfd.setType(analysisMap.get("qfdType").toString());
					qfd.setResults(qfdResultsString);
					
					getDaoManager().getToolsManager().addQFD(qfd);
				}else{
					existingQFD.setActivityName(analysisMap.get("activityName").toString());
					existingQFD.setWhats(whats);
					existingQFD.setHows(hows);
					existingQFD.setMatrixValues(qfdMatrixValues);
					existingQFD.setProductId(productId);
					existingQFD.setType(analysisMap.get("qfdType").toString());
					existingQFD.setResults(qfdResultsString);
					
					getDaoManager().getToolsManager().updateQFD(existingQFD);
				}
				
				
			}catch(Exception e){
				e.printStackTrace();
			}
			
			putOnState(ControllerConstants.NEXT_PAGE, "content/project/projectPage.jsp");
            putOnSession("currentTab", "tab5");
			
			return qfdResultsString;
			
		case ToolsConstants.DFX:
			try{
				String[] guidelinesIds = (String[])analysisMap.get("guidelinesIds");
				for(int i=0; i<guidelinesIds.length; i++){
					DfXGuidelines dfxG = getDaoManager().getDataAccessManager().getSpecificGuidelineById(Integer.valueOf(guidelinesIds[i]));
					Product product = getDaoManager().getProductManager().getProductById(productId);
					DfXRelMatrix dfxRM = new DfXRelMatrix();
					dfxRM.setActivityName(analysisMap.get("activityName").toString());
					dfxRM.setDfxGuideline(dfxG);
					dfxRM.setProduct(product);
					
					getDaoManager().getToolsManager().addDfXRelMatrix(dfxRM);
				}
				
			}catch(Exception e){
				e.printStackTrace();
			}
			
			putOnState(ControllerConstants.NEXT_PAGE, "content/project/projectPage.jsp");
            putOnSession("currentTab", "tab5");
            break;
            
		case ToolsConstants.REMOVE_DFXRM:
			getDaoManager().getToolsManager().deleteDfXRelMatrix(Integer.valueOf(analysisMap.get("dfxRMId").toString()));
			
			putOnState(ControllerConstants.NEXT_PAGE, "content/project/projectPage.jsp");
            putOnSession("currentTab", "tab5");
            break;
			
		}
		
	return null;
	}

}
